import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
import java.util.*;
import java.awt.*;


/**
  * This class implements a RowHeader for the Table.
  * Swing doesn't currently only provides a ColumnHeader, 
  * not a row header.  This is a very simple implementation
  * of a Row header.
  *
  * @see javax.swing.JTable
  * @see javax.swing.JScrollPane
  * @author Steve Wilson
  */
public class SpreadsheetRowHeader extends JTable {

   TableCellRenderer render = new RowHeaderRenderer();

   public SpreadsheetRowHeader(JTable table) {
      super(new RowHeaderModel(table));
      configure(table);
   }

   protected void configure(JTable table) {
      setRowHeight(table.getRowHeight());
      setIntercellSpacing(new Dimension(0,0));
      setShowHorizontalLines(false);
      setShowVerticalLines(false);
   }

   public Dimension getPreferredScrollableViewportSize() {
      return new Dimension(32, super.getPreferredSize().height);
   }             

   public TableCellRenderer getDefaultRenderer(Class c) {
      return render;
   }

   static class RowHeaderModel extends AbstractTableModel {
      JTable table;

      protected RowHeaderModel(JTable tableToMirror) {
         table = tableToMirror;
      }

      public int getRowCount() {
         return table.getModel().getRowCount();
      }

      public int getColumnCount() {
         return 1;
      }

      public Object getValueAt(int row, int column) {
         return String.valueOf(row+1);
      }
   }

   static class RowHeaderRenderer extends DefaultTableCellRenderer {
      public Component getTableCellRendererComponent(JTable table, Object value,
                                                     boolean isSelected, 
                                                     boolean hasFocus, 
                                                     int row, int column) {
         setBackground(UIManager.getColor("TableHeader.background"));
         setForeground(UIManager.getColor("TableHeader.foreground"));
         setBorder(UIManager.getBorder("TableHeader.cellBorder"));
         setFont(UIManager.getFont("TableHeader.font"));
         setValue(value);
         return this;
      }
   }
}
